package kr.jin.zeroprojectone;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
//pause()에서 timeString = start 해주기???
//db 없을때 목록열면 ???
//drop table후의 db재생성???
//목표시간
//날짜별 구분
//문서화??
//2단계 DB리스트 분단위
public class MainTime extends Activity {
	View mPage1, mPage2, mPage3, mPage4;
	ArrayList<MyItems> arList1 = new ArrayList<MyItems>();
	ArrayList<MyItems3> arList2; //4단계  
	ArrayList<MyItems3> arList3;//3단계
	//4단계 입력창에서 입력받아 result로 넘어온 값
	static ArrayList<String> textList11;
	static ArrayList<String> textList22;
	static ArrayList<String> textList33;
	
	static int PAGE = 1;//현재 페이지를 알기 위한 변수
	
	
	TextView nameText;//활동명
	TextView timeText;//시간
	TextView nameText2;//활동명2
	TextView timeText2;//시간2
	TextView nameText3;//활동명3
	TextView timeText3;//시간3
	String nameText4;//활동명4
	String firstTimeText4="00시간00분00초";//초기시간4
	String lastTimeText4;//onpause에서 저장하는 실제총시
	String nameText6;//활동명6
	String firstTimeText6="00시간00분00초";//초기시간6
	String lastTimeText6;//onpause에서 저장하는 실제총시간
	//시간
	long mBaseTime;
	long mPaseTime;
	
	String mBaseDate1;//저장시 날짜구분을 위해서
	String mBaseDate11;//저장시 날짜구분을 위해서
	
	//시간2
	long mBaseTime2;
	long mPaseTime2;
	//시간3
	long mBaseTime3;
	long mPaseTime3;
	//시간4
	long mBaseTime4;
	long mPaseTime4;
	//시간6
	long mBaseTime6;
	long mPaseTime6;
	
	long time;
	long time2;
	long time3;
	long time4;
	long time6;
	
	String nowTime;//3단계 시작 시간
	String nowTime2;//4단계 시작 시간
	
	//다이얼로그1
	final static int START = 0;
	final static int RUNNING = 1;
	final static int PAUSE = 2;
	static int timeStatus = START;
	//다이얼로그2
	final static int START2 = 3;
	final static int RUNNING2 = 4;
	final static int PAUSE2 = 5;
	static int timeStatus2 = START2;
	//다이얼로그3
	final static int START3 = 6;
	final static int RUNNING3 = 7;
	final static int PAUSE3 = 8;
	static int timeStatus3 = START3;
	//다이얼로그4
	final static int START4 = 9;
	final static int RUNNING4 = 10;
	final static int PAUSE4 = 11;
	static int timeStatus4 = START4;
	//다이얼로그6
	final static int START6 = 15;
	final static int RUNNING6 = 16;
	final static int PAUSE6 = 17;
	static int timeStatus6 = START6;
	//타이틀날짜
	TextView dateText1;
	TextView dateText2;
	TextView dateText3;
	TextView dateText4;
	String todayDate1;
	String todayDate2;
	String todayDate3;
	String todayDate4;
	//DB
	DBHelper dbHelper;
	
	ProgressBar pb;//3단계 프로그래스바
	Button clickBox;//3단계 클릭해주세요
	
	ProgressBar pb6;//4단계 프로그래스바
	Button clickBox6;//4단계 클릭해주세요
	
	LinearLayout linear;//루트 레이아웃
	
	Button maintimeBtn; //측정 버튼
	
	static Boolean ifif=true;//메인페이지 적용을 처음 시작할때만 하기 위한 변수 onResume() 의 setMainPage()에 적용
	
	int dateStatus1 = 0;//날짜가 하루 지나는 동안에도 기록이 될때 날짜 변동을 알리기 위한 변수
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		/*timeStatus = START;
		timeStatus2 = START2;
		timeStatus3 = START3;*/
		super.onCreate(savedInstanceState);
		/*Window win = getWindow();
		win.requestFeature(Window.FEATURE_NO_TITLE);*/
		setContentView(R.layout.maintime);
		
		ifif=true;//앱을 껐다가 다시 들어왔을때 메인페이지 설정값을 적용하기 위해서 
		
		mPage1 = findViewById(R.id.frame_layout1);
		mPage2 = findViewById(R.id.frame_layout2);
		mPage3 = findViewById(R.id.frame_layout3);
		mPage4 = findViewById(R.id.frame_layout4);
		
		nameText = (TextView)findViewById(R.id.frame_click1);
		timeText = (TextView)findViewById(R.id.frame_time1);
		nameText2 = (TextView)findViewById(R.id.frame_click2);
		timeText2 = (TextView)findViewById(R.id.frame_time2);
		nameText3 = (TextView)findViewById(R.id.frame_click3);
		timeText3 = (TextView)findViewById(R.id.frame_time3);
		
		clickBox = (Button)findViewById(R.id.frame_click4);
		clickBox6 = (Button)findViewById(R.id.frame_click6);
		
		linear = (LinearLayout) findViewById(R.id.linear_root);
		
		arList1= new ArrayList<MyItems>();
		arList2 = new ArrayList<MyItems3>();
		arList3 = new ArrayList<MyItems3>();
		//리스트 뷰
		MyListAdapter3 myAdapter3 = new MyListAdapter3(this, R.layout.customlistview3, arList3);//3단계
		MyListAdapter myAdapter1 = new MyListAdapter(this, R.layout.customlistview, arList1);//4단계 첫번째 리스트 뷰
		MyListAdapter2 myAdapter2 = new MyListAdapter2(this, R.layout.customlistview2, arList2);//4단계 두번째 리스트 뷰
		//3단계
		ListView ThirdStepList = (ListView)findViewById(R.id.third_step_list);
		ThirdStepList.setAdapter(myAdapter3);
		ThirdStepList.setOnItemClickListener(mItemListener7);
		//4단계 첫번째 리스트 뷰
		ListView FourthStepList1 = (ListView)findViewById(R.id.fourth_step_list1);
		FourthStepList1.setAdapter(myAdapter1);
		//4단계 두번째 리스트 뷰
		ListView FourthStepList2 = (ListView)findViewById(R.id.fourth_step_list2);
		FourthStepList2.setAdapter(myAdapter2);	
		FourthStepList2.setOnItemClickListener(mItemListener9);
		
		//타이틀날짜
		dateText1 = (TextView)findViewById(R.id.date_text1);
		dateText2 = (TextView)findViewById(R.id.date_text2);
		dateText3 = (TextView)findViewById(R.id.date_text3);
		dateText4 = (TextView)findViewById(R.id.date_text4);
		dateText1.setText(getTodayDate());
		dateText2.setText(getTodayDate());
		dateText3.setText(getTodayDate());
		dateText4.setText(getTodayDate());
		
		//DB
		dbHelper = new DBHelper(this);	
		
		//프로그래스바3단계
		pb = (ProgressBar) findViewById(R.id.progressbar_step3);
		//프로그래스바4단계
		pb6 = (ProgressBar) findViewById(R.id.progressbar_step4);
		
		maintimeBtn = (Button) findViewById(R.id.maintime_btn1);
		
		
	}
	//3단계
	private AdapterView.OnItemClickListener mItemListener7 = new AdapterView.OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			showDeleteDalog(7, position);
		}
	};
	
	//4단계두번째 리스트뷰
	private AdapterView.OnItemClickListener mItemListener9 = new AdapterView.OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			showDeleteDalog(9, position);
		}
	};
	
	void showDeleteDalog(int num, int position){
		switch(num){
		case 7://3단계 
			FragmentManager fm2 = getFragmentManager();
			FragmentTransaction tr2 = fm2.beginTransaction();
			Fragment fg2 = fm2.findFragmentByTag("delete7");
			if(fg2!=null){
				tr2.remove(fg2);
			}
			FragmentDialogDeleteList fd2 = FragmentDialogDeleteList.newInstance(num, position);
			fd2.show(tr2, "delete7");
			break;
		case 9://4단계 두번째 리스트뷰
			FragmentManager fm3 = getFragmentManager();
			FragmentTransaction tr3 = fm3.beginTransaction();
			Fragment fg3 = fm3.findFragmentByTag("delete9");
			if(fg3!=null){
				tr3.remove(fg3);
			}
			FragmentDialogDeleteList fd3 = FragmentDialogDeleteList.newInstance(num, position);
			fd3.show(tr3, "delete9");
			break;
		}	
	}
	
	
	void DeleteListPositiveDialog(int num, int position){
		String deletelistToast1 = getResources().getString(R.string.class_maintime_deletelist_toast1);
		String deletelistToast2 = getResources().getString(R.string.class_maintime_deletelist_toast2);
		
		switch(num){
		case 7://3단계
			if(timeStatus4==RUNNING4&&position == arList3.size()-1){
				Toast.makeText(MainTime.this, deletelistToast1, Toast.LENGTH_SHORT).show();
			}else if(timeStatus4==PAUSE4&&position == arList3.size()-1){
				Toast.makeText(MainTime.this, deletelistToast2, Toast.LENGTH_SHORT).show();
			}else{
				arList3.remove(position);
			}		
			setAdater3();
			break;
		case 9://4단계 두번째 리스트 뷰
			if(timeStatus6==RUNNING6&&position == arList2.size()-1){
				Toast.makeText(MainTime.this, deletelistToast1, Toast.LENGTH_SHORT).show();
			}else if(timeStatus6==PAUSE6&&position == arList2.size()-1){
				Toast.makeText(MainTime.this, deletelistToast2, Toast.LENGTH_SHORT).show();
			}else{
				arList2.remove(position);
			}		
			setAdater2();
			break;
		}
	};
	

	@Override
	protected void onResume() {
		super.onResume();
		String clickText = getResources().getString(R.string.class_maintime_click);
		String writingText = getResources().getString(R.string.class_maintime_writing);
		String waitingText = getResources().getString(R.string.class_maintime_waiting);
		String actName = getResources().getString(R.string.class_maintime_actname);
		
		
		if(ifif){
			setMainPage();
			ifif=false;
		}
		
		
		//페이지 상태 호출
		SharedPreferences pref_status_T = getSharedPreferences("status_T",0);
		timeStatus = pref_status_T.getInt("timeStatus", timeStatus);
		SharedPreferences pref_status_T2 = getSharedPreferences("status_T",0);
		timeStatus2 = pref_status_T2.getInt("timeStatus2", timeStatus2);
		SharedPreferences pref_status_T3 = getSharedPreferences("status_T",0);
		timeStatus3 = pref_status_T3.getInt("timeStatus3", timeStatus3);
		SharedPreferences pref_status_T4 = getSharedPreferences("status_T",0);
		timeStatus4 = pref_status_T4.getInt("timeStatus4", timeStatus4);
		SharedPreferences pref_status_T6 = getSharedPreferences("status_T",0);
		timeStatus6 = pref_status_T6.getInt("timeStatus6", timeStatus6);
		//1단계
		//활동명 호출
		SharedPreferences pref_inputAct = getSharedPreferences("inputAct", 0);
		nameText.setText(pref_inputAct.getString("LastAct", clickText));
		//상태별 호출
		switch(timeStatus){
		case START:
			break;
		case RUNNING:
			SharedPreferences pref_base_T_R= getSharedPreferences("base_T",0);
			mBaseTime = pref_base_T_R.getLong("mBaseTime", 0);
			SharedPreferences pref_mBaseDate1_R = getSharedPreferences("mBaseDate",0);
			mBaseDate1 = pref_mBaseDate1_R.getString("mBaseDate1", "0");
			mTimer.sendEmptyMessage(0);
			break;
		case PAUSE:
			SharedPreferences pref_base_T_P = getSharedPreferences("base_T",0);
			mBaseTime = pref_base_T_P.getLong("mBaseTime", 0);
			SharedPreferences pref_Pause_T = getSharedPreferences("Pause_T",0);
			mPaseTime = pref_Pause_T.getLong("mPaseTime", 0);
			SharedPreferences pref_mBaseDate1_P = getSharedPreferences("mBaseDate",0);
			mBaseDate1 = pref_mBaseDate1_P.getString("mBaseDate1", "0");
			timeText.setText(pref_Pause_T.getString("timeText", "00:00:00"));
			break;
		}	
		//2단계-1
		SharedPreferences pref_inputAct2 = getSharedPreferences("inputAct2", 0);
		nameText2.setText(pref_inputAct2.getString("LastAct2", clickText));
		switch(timeStatus2){
			case START2:
				break;
			case RUNNING2:
				SharedPreferences pref_base_T_R2= getSharedPreferences("base_T",0);
				mBaseTime2 = pref_base_T_R2.getLong("mBaseTime2", 0);
				mTimer2.sendEmptyMessage(0);
				break;
			case PAUSE2:
				SharedPreferences pref_base_T_P2 = getSharedPreferences("base_T",0);
				mBaseTime2 = pref_base_T_P2.getLong("mBaseTime2", 0);
				SharedPreferences pref_Pause_T2 = getSharedPreferences("Pause_T",0);
				mPaseTime2 = pref_Pause_T2.getLong("mPaseTime2", 0);
				timeText2.setText(pref_Pause_T2.getString("timeText2", "00:00:00"));
				break;
		}
		
		//2단계-2	
		SharedPreferences pref_inputAct3 = getSharedPreferences("inputAct3", 0);
		nameText3.setText(pref_inputAct3.getString("LastAct3", clickText));
		switch(timeStatus3){
			case START3:				
				break;
			case RUNNING3:				
				SharedPreferences pref_base_T_R3= getSharedPreferences("base_T",0);
				mBaseTime3 = pref_base_T_R3.getLong("mBaseTime3", 0);
				mTimer3.sendEmptyMessage(0);
				break;
			case PAUSE3:				
				SharedPreferences pref_base_T_P3 = getSharedPreferences("base_T",0);
				mBaseTime3 = pref_base_T_P3.getLong("mBaseTime3", 0);
				SharedPreferences pref_Pause_T3 = getSharedPreferences("Pause_T",0);
				mPaseTime3 = pref_Pause_T3.getLong("mPaseTime3", 0);
				timeText3.setText(pref_Pause_T3.getString("timeText3", "00:00:00"));
				break;
		}	
		
		/*if(timeStatus4 == RUNNING4){
			clickBox.setText("기록중");
		}else if(timeStatus4 == PAUSE4){
			clickBox.setText("대기중");
		}else{
			clickBox.setText("클릭해주세요");
		}*/
		//3단계 	
		SharedPreferences pref_inputAct4 = getSharedPreferences("inputAct4", 0);
		nameText4 = pref_inputAct4.getString("LastAct4", actName);
		switch(timeStatus4){
			case START4:
				clickBox.setText(clickText);
				pb.setVisibility(View.INVISIBLE);//프로그래스바 숨기기
				readArraytoFile4();	
				setAdater3();
				break;
			case RUNNING4:
				clickBox.setText(writingText);
				pb.setVisibility(View.VISIBLE);//프로그래스바 보이기
				//파일에서 arrayList값 가져옴
				readArraytoFile4();			
				//마지막으로 저장된 시작 시간과 실제 총시간
				SharedPreferences pref_now_T4 = getSharedPreferences("now_T",0);
				nowTime = pref_now_T4.getString("nowTime", "0");
				//lastTimeText4 = pref_now_T4.getString("lastTimeText4", "0");
					
				SharedPreferences pref_base_T_R4= getSharedPreferences("base_T",0);
				mBaseTime4 = pref_base_T_R4.getLong("mBaseTime4", 0);
				mTimer4.sendEmptyMessage(0);
				break;
			case PAUSE4:
				clickBox.setText(waitingText);
				readArraytoFile4();
				SharedPreferences pref_now_T44 = getSharedPreferences("now_T",0);
				nowTime = pref_now_T44.getString("nowTime", "0");
				pb.setVisibility(View.INVISIBLE);//프로그래스바 숨기기
				SharedPreferences pref_base_T_P4 = getSharedPreferences("base_T",0);
				mBaseTime4 = pref_base_T_P4.getLong("mBaseTime4", 0);
				SharedPreferences pref_Pause_T4 = getSharedPreferences("Pause_T",0);
				mPaseTime4 = pref_Pause_T4.getLong("mPaseTime4", 0);
				
				setAdater3();
				
				break;
		}		
		
		//4단계-1
		/*if(textList33!=null){
			arList1.clear();
			for(int i=0; i<textList33.size(); i++){
				arList1.add(new MyItems(textList33.get(i).trim(),textList11.get(i).trim(),"---",textList22.get(i).trim()));
			}
		}
		setAdater1();*/
		if(getSharedPreferences("schedule", 0)!=null){
			set4StepSchedule();
		}
		
		
		//4단계 -2
		SharedPreferences pref_inputAct6 = getSharedPreferences("inputAct6", 0);
		nameText6 = pref_inputAct6.getString("LastAct6", actName);
		switch(timeStatus6){
			case START6:
				clickBox6.setText(clickText);
				pb6.setVisibility(View.INVISIBLE);//프로그래스바 숨기기
				readArraytoFile6();	
				setAdater2();
				break;
			case RUNNING6:
				clickBox6.setText(writingText);
				pb6.setVisibility(View.VISIBLE);//프로그래스바 보이기
				readArraytoFile6();	
				SharedPreferences pref_now_T6 = getSharedPreferences("now_T",0);
				nowTime2 = pref_now_T6.getString("nowTime2", "0");
				//lastTimeText6 = pref_now_T6.getString("lastTimeText6", "0");
				
				SharedPreferences pref_base_T_R6= getSharedPreferences("base_T",0);
				mBaseTime6 = pref_base_T_R6.getLong("mBaseTime6", 0);
				mTimer6.sendEmptyMessage(0);
				break;
			case PAUSE6:
				readArraytoFile6();	
				clickBox6.setText(waitingText);
				pb6.setVisibility(View.INVISIBLE);//프로그래스바 숨기기
				SharedPreferences pref_now_T66 = getSharedPreferences("now_T",0);
				nowTime2 = pref_now_T66.getString("nowTime2", "0");
				SharedPreferences pref_base_T_P6 = getSharedPreferences("base_T",0);
				mBaseTime6 = pref_base_T_P6.getLong("mBaseTime6", 0);
				SharedPreferences pref_Pause_T6 = getSharedPreferences("Pause_T",0);
				mPaseTime6 = pref_Pause_T6.getLong("mPaseTime6", 0);
				setAdater2();
				break;
		}
	}
	
	//예상스케쥴의 저장된 데이터를 메인페이지 4단계에 뿌려주는 메서드
	private void set4StepSchedule(){
		SharedPreferences pref1 = getSharedPreferences("schedule", 0);
		textList33 = new ArrayList<String>();
		textList22 = new ArrayList<String>();
		textList11 = new ArrayList<String>();
		textList33.clear();
		int size3 = pref1.getInt("lastSize3", 0);
		for(int i=0;i<size3;i++){
			textList33.add(pref1.getString("last_status3"+i, ""));
		}
		
		textList22.clear();
		int size2 = pref1.getInt("lastSize2", 0);
		for(int i=0;i<size2;i++){
			textList22.add(pref1.getString("last_status2"+i, ""));
		}
				
		textList11.clear();
		int size1 = pref1.getInt("lastSize1", 0);
		for(int i=0;i<size1;i++){
			textList11.add(pref1.getString("last_status1"+i, ""));
		}
		arList1.clear();
		for(int i=0; i<textList33.size(); i++){
			arList1.add(new MyItems(textList33.get(i).trim(),textList11.get(i).trim(),"---",textList22.get(i).trim()));
		}
	
		MyListAdapter myAdapter1 = new MyListAdapter(this, R.layout.customlistview, arList1);
		ListView FourthStepList1 = (ListView)findViewById(R.id.fourth_step_list1);
		FourthStepList1.setAdapter(myAdapter1);
	}
	
	
	
	//설정페이지에서 설정한 메인페이지를  적용하는 메소드
	private void setMainPage() {
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
		
		int mainPage = Integer.parseInt(pref.getString("mainsetting", "1"));//메인페이지를 설정하기 위한 변수
		//Toast.makeText(this, mainPage, Toast.LENGTH_LONG).show();
		
		mPage1.setVisibility(View.INVISIBLE);
		mPage2.setVisibility(View.INVISIBLE);
		mPage3.setVisibility(View.INVISIBLE);
		mPage4.setVisibility(View.INVISIBLE);
		switch(mainPage){
		case 1:
			mPage1.setVisibility(View.VISIBLE);
			linear.setBackgroundResource(R.drawable.ttoc1);
			//maintimeBtn.setText("1단계");
			PAGE = 1;//현재 페이지를 알기위한 변수
			break;						
		case 2:
			mPage2.setVisibility(View.VISIBLE);
			linear.setBackgroundResource(R.drawable.ttoc2);
			//maintimeBtn.setText("2단계");
			PAGE = 2;
			break;
		case 3:						
			mPage3.setVisibility(View.VISIBLE);
			linear.setBackgroundResource(R.drawable.ttoc3);
			//maintimeBtn.setText("3단계");
			PAGE = 3;
			break;
		case 4:
			mPage4.setVisibility(View.VISIBLE);
			linear.setBackgroundResource(R.drawable.ttoc4);
			//maintimeBtn.setText("4단계");
			PAGE = 4;
			break;
		}
	}


	private void setAdater1() {
		MyListAdapter myAdapter1 = new MyListAdapter(this, R.layout.customlistview, arList1);
		ListView FourthStepList1 = (ListView)findViewById(R.id.fourth_step_list1);
		FourthStepList1.setAdapter(myAdapter1);
	}
	
	private void setAdater2() {
		MyListAdapter2 myAdapter2 = new MyListAdapter2(this, R.layout.customlistview2, arList2);
		ListView FourthStepList2 = (ListView)findViewById(R.id.fourth_step_list2);
		FourthStepList2.setAdapter(myAdapter2);	
		
	}

	private void setAdater3() {
		MyListAdapter3 myAdapter3 = new MyListAdapter3(MainTime.this, R.layout.customlistview3, arList3);
		ListView ThirdStepList = (ListView)findViewById(R.id.third_step_list);
		ThirdStepList.setAdapter(myAdapter3);	
	}
	
	private void readArraytoFile6() {
		arList2.clear();
		String fileName = "ArrayList2";
		File arList2File = getFileStreamPath(fileName);
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		try {
			fis = openFileInput(fileName);
			ois = new ObjectInputStream(fis);
			arList2 = (ArrayList<MyItems3>) ois.readObject();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally{
			try{fis.close();}catch(Exception e){}
		}
		
	}
	
	private void readArraytoFile4() {
		arList3.clear();
		String fileName = "ArrayList3";
		File arList3File = getFileStreamPath(fileName);
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		try {
			fis = openFileInput(fileName);
			ois = new ObjectInputStream(fis);
			arList3 = (ArrayList<MyItems3>) ois.readObject();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally{
			try{fis.close();}catch(Exception e){}
		}
		
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		OnPauseThread opt = new OnPauseThread();
		opt.start();
	}
	class OnPauseThread extends Thread{
		@Override
		public void run() {
			Pause();
			
		}
	}
	private void Pause() {
		//페이지 상태 저장
		SharedPreferences pref_status_T = getSharedPreferences("status_T",0);
		SharedPreferences.Editor editor_status_T = pref_status_T.edit();
		editor_status_T.putInt("timeStatus", timeStatus);
		editor_status_T.putInt("timeStatus2", timeStatus2);
		editor_status_T.putInt("timeStatus3", timeStatus3);
		editor_status_T.putInt("timeStatus4", timeStatus4);
		editor_status_T.putInt("timeStatus6", timeStatus6);
		editor_status_T.commit();
		//mBaseTime 저장
		SharedPreferences pref_base_T = getSharedPreferences("base_T",0);
		SharedPreferences.Editor editor_base_T = pref_base_T.edit();
		editor_base_T.putLong("mBaseTime", mBaseTime);
		editor_base_T.putLong("mBaseTime2", mBaseTime2);
		editor_base_T.putLong("mBaseTime3", mBaseTime3);
		editor_base_T.putLong("mBaseTime4", mBaseTime4);
		editor_base_T.putLong("mBaseTime6", mBaseTime6);
		editor_base_T.commit();		
		
		//1단계
		switch(timeStatus){
		case START:
			break;
		case RUNNING:
			SharedPreferences pref_mBaseDate = getSharedPreferences("mBaseDate",0);
			mBaseDate1=pref_mBaseDate.getString("mBaseDate1", getTodayDate());
			break;
		case PAUSE:
			String tt = timeText.getText().toString();
			SharedPreferences pref_Pause_T_P2 = getSharedPreferences("Pause_T",0);
			SharedPreferences.Editor editor_Pause_T_P2 = pref_Pause_T_P2.edit();
			editor_Pause_T_P2.putLong("mPaseTime", mPaseTime);
			editor_Pause_T_P2.putString("timeText", tt);
			editor_Pause_T_P2.commit();	
			break;	
		}
		
		switch(timeStatus2){
		case START2:
			break;
		case RUNNING2:
			break;
		case PAUSE2:
			String tt2 = timeText2.getText().toString();
			SharedPreferences pref_Pause_T_P = getSharedPreferences("Pause_T",0);
			SharedPreferences.Editor editor_Pause_T_P = pref_Pause_T_P.edit();
			editor_Pause_T_P.putLong("mPaseTime2", mPaseTime2);
			editor_Pause_T_P.putString("timeText2", tt2);
			editor_Pause_T_P.commit();	
			break;	
		}
		
		switch(timeStatus3){
		case START3:
			break;
		case RUNNING3:
			break;
		case PAUSE3:
			String tt3 = timeText3.getText().toString();
			SharedPreferences pref_Pause_T_P3 = getSharedPreferences("Pause_T",0);
			SharedPreferences.Editor editor_Pause_T_P3 = pref_Pause_T_P3.edit();
			editor_Pause_T_P3.putLong("mPaseTime3", mPaseTime3);
			editor_Pause_T_P3.putString("timeText3", tt3);
			editor_Pause_T_P3.commit();	
			break;			
		}		
		
		switch(timeStatus4){
		case START4:
			writeArraytoFile4();
			break;
		case RUNNING4:	
			writeArraytoFile4();
			
			//lastTimeText4 = startTime4();
			SharedPreferences pref_now_T4 = getSharedPreferences("now_T",0);
			SharedPreferences.Editor editor_now_T4 = pref_now_T4.edit();
			editor_now_T4.putString("nowTime", nowTime);
			//editor_now_T4.putString("lastTimeText4", lastTimeText4);
			editor_now_T4.commit();
			
			break;
		case PAUSE4:
			writeArraytoFile4();
			SharedPreferences pref_now_T44 = getSharedPreferences("now_T",0);
			SharedPreferences.Editor editor_now_T44 = pref_now_T44.edit();
			editor_now_T44.putString("nowTime", nowTime);
			editor_now_T44.commit();
			SharedPreferences pref_Pause_T_P4 = getSharedPreferences("Pause_T",0);
			SharedPreferences.Editor editor_Pause_T_P4 = pref_Pause_T_P4.edit();
			editor_Pause_T_P4.putLong("mPaseTime4", mPaseTime4);
			editor_Pause_T_P4.commit();
			
			break;			
		}		
		
		//4단계-2
		switch(timeStatus6){
		case START6:
			writeArraytoFile6();
			break;
		case RUNNING6:
			writeArraytoFile6();
			
			//lastTimeText6 = startTime6();
			SharedPreferences pref_now_T6 = getSharedPreferences("now_T",0);
			SharedPreferences.Editor editor_now_T6 = pref_now_T6.edit();
			editor_now_T6.putString("nowTime2", nowTime2);
			//editor_now_T6.putString("lastTimeText6", lastTimeText6);
			editor_now_T6.commit();
			break;
		case PAUSE6:
			writeArraytoFile6();
			SharedPreferences pref_now_T66 = getSharedPreferences("now_T",0);
			SharedPreferences.Editor editor_now_T66 = pref_now_T66.edit();
			editor_now_T66.putString("nowTime2", nowTime2);
			editor_now_T66.commit();
			SharedPreferences pref_Pause_T_P6 = getSharedPreferences("Pause_T",0);
			SharedPreferences.Editor editor_Pause_T_P6 = pref_Pause_T_P6.edit();
			editor_Pause_T_P6.putLong("mPaseTime6", mPaseTime6);
			editor_Pause_T_P6.commit();
			
			break;			
		}			
	}
		
	private void writeArraytoFile6() {
		String fileName = "ArrayList2";
		File arList2File = getFileStreamPath(fileName);
		FileOutputStream fos = null;
		ObjectOutputStream oos;
		try {
			fos = openFileOutput(fileName, MODE_PRIVATE);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(arList2);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try{fos.close();} catch(Exception e){}
		}		
		
	}

	private void writeArraytoFile4() {
		String fileName = "ArrayList3";
		File arList3File = getFileStreamPath(fileName);
		FileOutputStream fos = null;
		ObjectOutputStream oos;
		try {
			fos = openFileOutput(fileName, MODE_PRIVATE);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(arList3);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try{fos.close();} catch(Exception e){}
		}		
	}

	//년월일
	public String getTodayDate(){
		SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy.MM.dd");
		Calendar cal = Calendar.getInstance();
		int day = Calendar.DAY_OF_MONTH;
		return simpleDate.format(cal.getTime());
	}//getTodayDate()
	
	//월일
	public String getTodayDate2(){
		SimpleDateFormat simpleDate = new SimpleDateFormat("MM.dd");
		Calendar cal = Calendar.getInstance();
		int day = Calendar.DAY_OF_MONTH;
		return simpleDate.format(cal.getTime());
	}//getTodayDate2()
	
	//시간
	public String getNowTime(){
		Date now = new Date();
		SimpleDateFormat simpleDate = new SimpleDateFormat("HH:mm");
		return simpleDate.format(now);
	}//getNowTime()
	
	public void mOnClick(View v){
		PopupMenu popup = new PopupMenu(this, v);	
		switch(v.getId()){
		case R.id.maintime_btn1:	
			
			popup.inflate(R.menu.popupmenu);
			popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {			
				@Override
				public boolean onMenuItemClick(MenuItem item) {
					mPage1.setVisibility(View.INVISIBLE);
					mPage2.setVisibility(View.INVISIBLE);
					mPage3.setVisibility(View.INVISIBLE);
					mPage4.setVisibility(View.INVISIBLE);
					switch(item.getItemId()){
					case R.id.popupmenu_step1:
						mPage1.setVisibility(View.VISIBLE);
						//linear.setBackgroundResource(R.drawable.timetochange4);
						linear.setBackgroundResource(R.drawable.ttoc1);
						//maintimeBtn.setText("1단계");
						PAGE = 1;//현재 페이지를 알기위한 변수
						break;						
					case R.id.popupmenu_step2:
						mPage2.setVisibility(View.VISIBLE);
						//linear.setBackgroundResource(R.drawable.timetochange7);
						linear.setBackgroundResource(R.drawable.ttoc2);
						//maintimeBtn.setText("2단계");
						PAGE = 2;
						break;
					case R.id.Popupmenu_step3:						
						mPage3.setVisibility(View.VISIBLE);
						//linear.setBackgroundResource(R.drawable.timetochange6);
						linear.setBackgroundResource(R.drawable.ttoc3);
						//maintimeBtn.setText("3단계");
						PAGE = 3;
						break;
					case R.id.Popupmenu_step4:
						mPage4.setVisibility(View.VISIBLE);
						//linear.setBackgroundResource(R.drawable.timetochange5);
						linear.setBackgroundResource(R.drawable.ttoc4);
						//maintimeBtn.setText("4단계");
						PAGE = 4;
						break;
					}
					return false;
				}
			});
			popup.show();
			break;
		case R.id.maintime_btn2:
			Intent intent = new Intent();
			if(mPage1.getVisibility() == View.VISIBLE){
				intent.setClass(MainTime.this, DBListAcitiviryStep1.class);
			}else if(mPage2.getVisibility() == View.VISIBLE){
				intent.setClass(MainTime.this, DBListAcitiviryStep2.class);
			}else if(mPage3.getVisibility() == View.VISIBLE){
				intent.setClass(MainTime.this, DBListAcitiviryStep4.class);
			}else{
				intent.setClass(MainTime.this, DBListAcitiviryStep6.class);
			}
			/*switch(PAGE){
			case 1:
				intent.setClass(MainTime.this, DBListAcitiviryStep1.class);//클래스명의 step은 메뉴의 step과는 상관없음
				break;
			case 2:
				intent.setClass(MainTime.this, DBListAcitiviryStep2.class);
				break;
			case 3:
				intent.setClass(MainTime.this, DBListAcitiviryStep4.class);
				break;
			case 4:
				intent.setClass(MainTime.this, DBListAcitiviryStep6.class);
				break;
			}			*/
			startActivity(intent);
			break;
		case R.id.maintime_btn3:
			/*Intent intenti = new Intent(this, GraphLine1.class);
			startActivity(intenti);*/
			Toast.makeText(MainTime.this, "도움페이지를 호춣합니다.", Toast.LENGTH_SHORT).show();
			break;
		case R.id.maintime_btn4:
			Intent settingIntent = new Intent(this, SettingPage.class);
			startActivity(settingIntent);
			
			/*popup.inflate(R.menu.popupmenu2);
			popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {		
				@Override
				public boolean onMenuItemClick(MenuItem item) {				
					switch(item.getItemId()){
					case R.id.popupmenu2_step1:				
						DBHelper dbHelper= new DBHelper(MainTime.this);
						SQLiteDatabase db = dbHelper.getWritableDatabase();
						if(mPage1.getVisibility() == View.VISIBLE){
							db.execSQL("DELETE FROM step1");
							Toast.makeText(MainTime.this, "1단계 목록이 초기화 되었습니다.", Toast.LENGTH_SHORT).show();
						}else if(mPage2.getVisibility() == View.VISIBLE){
							db.execSQL("DELETE FROM step2");
							db.execSQL("DELETE FROM step3");
							Toast.makeText(MainTime.this, "2단계 목록이 초기화 되었습니다.", Toast.LENGTH_SHORT).show();
						}else if(mPage3.getVisibility() == View.VISIBLE){
							db.execSQL("DELETE FROM step4");
							Toast.makeText(MainTime.this, "3단계 목록이 초기화 되었습니다.", Toast.LENGTH_SHORT).show();
						}else if(mPage4.getVisibility()== View.VISIBLE){
							db.execSQL("DELETE FROM step6");
							Toast.makeText(MainTime.this, "4단계 목록이 초기화 되었습니다.", Toast.LENGTH_SHORT).show();
						}
						break;
					case R.id.popupmenu2_step2:
						dbHelper = new DBHelper(MainTime.this);
						SQLiteDatabase db2 = dbHelper.getWritableDatabase();
						db2.execSQL("DROP TABLE IF EXISTS step1");
						Toast.makeText(MainTime.this, "drop table", Toast.LENGTH_SHORT).show();
								
						break;
					case R.id.Popupmenu2_step3:
						Toast.makeText(MainTime.this, "설정페이지를 호춣합니다.", Toast.LENGTH_SHORT).show();										
						break;			
					}
					return false;
				}
			});
			popup.show();			*/
			break;
		}
	}//end of mOnClick()
	
	public void mOnClickInsert(View v){	
		switch(v.getId()){
		case R.id.frame_click1:
		case R.id.frame_time1:
			showInputDialog(1);
			break;
		case R.id.frame_click2:
		case R.id.frame_time2:
			showInputDialog(2);
			break;
		case R.id.frame_click3:
		case R.id.frame_time3:
			showInputDialog(3);
			break;
		case R.id.frame_click4:
			showInputDialog(4);
			break;
		case R.id.frame_click5:
			Intent intent = new Intent(this, InsertSchedule.class);
			startActivityForResult(intent, 0);
			break;
		case R.id.frame_click6:
			showInputDialog(6);
			break;
		}
	}//end of mOnClickInsert()
	
	//onresume에서 메인페이지에 뿌려주는 작업을 해줌
	/*@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		switch(requestCode){
		case 0:
			if(resultCode == RESULT_OK){
				textList11 = data.getStringArrayListExtra("textList1");
				textList22 = data.getStringArrayListExtra("textList2");
				textList33 = data.getStringArrayListExtra("textList3");
				
				for(int i=0; i<textList33.size(); i++){
					arList1.add(new MyItems(textList33.get(i).trim(),textList11.get(i).trim(),"---",textList22.get(i).trim()));
				
				}
				
				
				MyListAdapter myAdapter1 = new MyListAdapter(this, R.layout.customlistview, arList1);
				ListView FourthStepList1 = (ListView)findViewById(R.id.fourth_step_list1);
				FourthStepList1.setAdapter(myAdapter1);
			}
			break;
		}
		
	}//onActivityResult()
*/	
	public void showInputDialog(int num){	
		FragmentManager fm = getFragmentManager();
		FragmentTransaction tr = fm.beginTransaction();
		Fragment fg = fm.findFragmentByTag("dialog");
		Fragment fg2 = fm.findFragmentByTag("dialog2");
		Fragment fg3 = fm.findFragmentByTag("dialog3");
		Fragment fg4 = fm.findFragmentByTag("dialog4");
		Fragment fg5 = fm.findFragmentByTag("dialog5");
		Fragment fg6 = fm.findFragmentByTag("dialog6");
		if(fg != null){
			tr.remove(fg);
		}	
		if(fg2 != null){
			tr.remove(fg2);
		}	
		if(fg3 != null){
			tr.remove(fg3);
		}	
		if(fg4 != null){
			tr.remove(fg4);
		}	
		if(fg5 != null){
			tr.remove(fg5);
		}	
		if(fg6 != null){
			tr.remove(fg6);
		}	
		switch(num){
		case 1:
			switch(timeStatus){
			case START:
				FragmentDialog fd1 = new FragmentDialog();
				fd1.show(tr, "dialog");		
				break;
			case RUNNING:
				FragmentDialog fd11 = new FragmentDialog();
				fd11.show(tr, "dialog");
				break;
			case PAUSE:
				FragmentDialog fd111 = new FragmentDialog();
				fd111.show(tr, "dialog");
				break;
			}
			break;
		case 2:
			switch(timeStatus2){
			case START2:
				FragmentDialog2 fd2 = new FragmentDialog2();
				fd2.show(tr, "dialog2");		
				break;
			case RUNNING2:
				FragmentDialog2 fd22 = new FragmentDialog2();
				fd22.show(tr, "dialog2");
				break;
			case PAUSE2:
				FragmentDialog2 fd222 = new FragmentDialog2();
				fd222.show(tr, "dialog2");
				break;
			}
			break;
		case 3:
			switch(timeStatus3){
			case START3:
				FragmentDialog3 fd3 = new FragmentDialog3();
				fd3.show(tr, "dialog3");		
				break;
			case RUNNING3:
				FragmentDialog3 fd33 = new FragmentDialog3();
				fd33.show(tr, "dialog3");
				break;
			case PAUSE3:
				FragmentDialog3 fd333 = new FragmentDialog3();
				fd333.show(tr, "dialog3");
				break;
			}
			break;
		case 4:
			switch(timeStatus4){
			case START4:
				FragmentDialog4 fd4 = new FragmentDialog4();
				fd4.show(tr, "dialog4");		
				break;
			case RUNNING4:
				FragmentDialog4 fd44 = new FragmentDialog4();
				fd44.show(tr, "dialog4");
				break;
			case PAUSE4:
				FragmentDialog4 fd444 = new FragmentDialog4();
				fd444.show(tr, "dialog4");
				break;
			}
			break;
		case 5:
					
			break;
		case 6:
			switch(timeStatus6){
			case START6:
				FragmentDialog6 fd6 = new FragmentDialog6();
				fd6.show(tr, "dialog6");		
				break;
			case RUNNING6:
				FragmentDialog6 fd66 = new FragmentDialog6();
				fd66.show(tr, "dialog6");
				break;
			case PAUSE6:
				FragmentDialog6 fd666 = new FragmentDialog6();
				fd666.show(tr, "dialog6");
				break;
			}
			break;
		}		
	}//end of showInputDialog()
	
	public void doPositiveClick(int whichDialog, String name){
		String writingText = getResources().getString(R.string.class_maintime_writing);
		String waitingText = getResources().getString(R.string.class_maintime_waiting);
		
		switch(whichDialog){
		case 1:
			switch(timeStatus){
			case START:
				nameText.setText(name);		
				mBaseTime = SystemClock.elapsedRealtime();
				//mBaseDate1 (기록 시작 날짜)저장
				mBaseDate1 = getTodayDate();
				SharedPreferences pref_mBaseDate = getSharedPreferences("mBaseDate",0);
				SharedPreferences.Editor editor_mBaseDate = pref_mBaseDate.edit();
				editor_mBaseDate.putString("mBaseDate1", mBaseDate1);//비교를 위한 데이터
				editor_mBaseDate.putString("mBaseDate11", mBaseDate11);//저장을 위한 데이터
				editor_mBaseDate.commit();
				
				mTimer.sendEmptyMessage(0);
				timeStatus = RUNNING;	
				break;
			case RUNNING:
				mTimer.removeMessages(0);
				mPaseTime = SystemClock.elapsedRealtime();	
				timeStatus = PAUSE;
				break;
			case PAUSE:
				long now = SystemClock.elapsedRealtime();
				mBaseTime += (now-mPaseTime);
				mTimer.sendEmptyMessage(0);
				timeStatus = RUNNING;	
				break;
			}		
			break;
		case 2:
			switch(timeStatus2){
			case START2:
				nameText2.setText(name);	
				mBaseTime2 = SystemClock.elapsedRealtime();	
				mTimer2.sendEmptyMessage(0);
				timeStatus2 = RUNNING2;
				break;
			case RUNNING2:
				mTimer2.removeMessages(0);
				mPaseTime2 = SystemClock.elapsedRealtime();	
				timeStatus2 = PAUSE2;
				break;
			case PAUSE2:
				long now2 = SystemClock.elapsedRealtime();
				mBaseTime2 += (now2-mPaseTime2);
				mTimer2.sendEmptyMessage(0);
				timeStatus2 = RUNNING2;
				break;
			}		
			break;
		case 3:
			switch(timeStatus3){
			case START3:
				nameText3.setText(name);					
				mBaseTime3 = SystemClock.elapsedRealtime();	
				mTimer3.sendEmptyMessage(0);
				timeStatus3 = RUNNING3;
				break;
			case RUNNING3:
				mTimer3.removeMessages(0);
				mPaseTime3 = SystemClock.elapsedRealtime();	
				timeStatus3 = PAUSE3;
				break;
			case PAUSE3:
				long now3 = SystemClock.elapsedRealtime();
				mBaseTime3 += (now3-mPaseTime3);
				mTimer3.sendEmptyMessage(0);
				timeStatus3 = RUNNING3;
				break;
			}		
			break;
		case 4:
			switch(timeStatus4){
			case START4:
				clickBox.setText(writingText);
				nameText4 = name;	
				nowTime = getNowTime();//현재시간을 불러와 변수에 넣어둠, 시작시간을 저장하기 위해
				arList3.add(new MyItems3(nameText4,firstTimeText4, nowTime,"---",""));				
				mBaseTime4 = SystemClock.elapsedRealtime();	
				mTimer4.sendEmptyMessage(0);
				pb.setVisibility(View.VISIBLE);//프로그래스바 보이기
				timeStatus4 = RUNNING4;
				break;
			case RUNNING4:
				clickBox.setText(waitingText);
				mTimer4.removeMessages(0);
				mPaseTime4 = SystemClock.elapsedRealtime();	
				pb.setVisibility(View.INVISIBLE);//프로그래스바 숨기기
				timeStatus4 = PAUSE4;
				break;
			case PAUSE4:
				clickBox.setText(writingText);
				long now4 = SystemClock.elapsedRealtime();
				mBaseTime4 += (now4-mPaseTime4);
				mTimer4.sendEmptyMessage(0);
				pb.setVisibility(View.VISIBLE);//프로그래스바 보이기
				timeStatus4 = RUNNING4;
				break;
			}		
			break;
		case 5:
		
			break;
			
		case 6:
			switch(timeStatus6){
			case START6:
				clickBox6.setText(writingText);
				
				pb6.setVisibility(View.VISIBLE);//프로그래스바 보이기
				nameText6 = name;	
				nowTime2 = getNowTime();//현재시간을 불러와 변수에 넣어둠, 시작시간을 저장하기 위해
				arList2.add(new MyItems3(nameText6,firstTimeText6,nowTime2,"---",""));				
				mBaseTime6 = SystemClock.elapsedRealtime();	
				mTimer6.sendEmptyMessage(0);
				
				timeStatus6 = RUNNING6;
				break;
			case RUNNING6:
				clickBox6.setText(waitingText);
				pb6.setVisibility(View.INVISIBLE);//프로그래스바 숨기기
				mTimer6.removeMessages(0);
				mPaseTime6 = SystemClock.elapsedRealtime();	
				timeStatus6 = PAUSE6;
				break;
			case PAUSE6:
				clickBox6.setText(writingText);
				pb6.setVisibility(View.VISIBLE);//프로그래스바 보이기
				long now6 = SystemClock.elapsedRealtime();
				mBaseTime6 += (now6-mPaseTime6);
				mTimer6.sendEmptyMessage(0);
				timeStatus6 = RUNNING6;
				break;
			}		
			break;	
		}
		
	}//end of doPositiveClick()
	
	public void doNeutralClick(int whichDialog,String name){
		String firstTime = getResources().getString(R.string.class_maintime_first_time);
		String clickText = getResources().getString(R.string.class_maintime_click);
		String date = getResources().getString(R.string.class_maintime_date);
		
		switch(whichDialog){
		case 1:
			switch(timeStatus){
			case START:
				break;
			case RUNNING:			
				break;
			case PAUSE:
				SharedPreferences pref_mBaseDate = getSharedPreferences("mBaseDate",0);
				mBaseDate11=pref_mBaseDate.getString("mBaseDate11", getTodayDate());
				mTimer.removeMessages(0);
				timeText.setText(firstTime);
				timeStatus=START;
				//db
				dbHelper = new DBHelper(this);
				SQLiteDatabase db = dbHelper.getWritableDatabase();
				long a = mPaseTime-mBaseTime;
				//db.execSQL("INSERT INTO step1 VALUES( null,'" + todayDate1 + "','" + name + "','" + a + "');");
				//db.execSQL("INSERT INTO step1 VALUES( null,'" + getTodayDate() + "','" + name + "','" + a + "');");
				//날짜 변경상태를 확인하기 위해서
				SharedPreferences pref_dateStatus11 = getSharedPreferences("dateStatus",0);
				pref_dateStatus11.getInt("dateStatus1", 0);
				if(dateStatus1==0){
					db.execSQL("INSERT INTO step1 VALUES( null,'" + mBaseDate11 + "','" + name + "','" + a + "');");
				}else{
					db.execSQL("INSERT INTO step1 VALUES( null,'" + mBaseDate11+"+"+dateStatus1 + "','" + name + "','" + a + "');");
					dateStatus1=0;
					SharedPreferences pref_dateStatus1 = getSharedPreferences("dateStatus",0);
					SharedPreferences.Editor editor_dateStatus1 = pref_dateStatus1.edit();
					editor_dateStatus1.putInt("dateStatus1", dateStatus1);
					editor_dateStatus1.commit();
				}
				
				dbHelper.close();
				break;
			}
			break;
		case 2:
			switch(timeStatus2){
			case START2:
				break;
			case RUNNING2:		
				break;
			case PAUSE2:
				mTimer2.removeMessages(0);
				pb.setVisibility(View.INVISIBLE);//프로그래스바 보이기<-------------------------?????????????
				timeText2.setText(firstTime);
				timeStatus2 = START2;
				//db
				dbHelper = new DBHelper(this);
				SQLiteDatabase db = dbHelper.getWritableDatabase();
				long a = mPaseTime2-mBaseTime2;
				//db.execSQL("INSERT INTO step1 VALUES( null,'" + todayDate1 + "','" + name + "','" + a + "');");
				db.execSQL("INSERT INTO step2 VALUES( null,'" + getTodayDate() + "','" + name + "','" + a + "');");
				dbHelper.close();
				break;
			}
			break;
		
		case 3:
		
			switch(timeStatus3){
			case START3:
				break;
			case RUNNING3:		
				break;
			case PAUSE3:
				mTimer3.removeMessages(0);
				timeText3.setText(firstTime);
				timeStatus3=START3;
				//db
				dbHelper = new DBHelper(this);
				SQLiteDatabase db = dbHelper.getWritableDatabase();
				long a = mPaseTime3-mBaseTime3;
				//db.execSQL("INSERT INTO step1 VALUES( null,'" + todayDate1 + "','" + name + "','" + a + "');");
				db.execSQL("INSERT INTO step3 VALUES( null,'" + getTodayDate() + "','" + name + "','" + a + "');");
				dbHelper.close();
				break;
			}
			break;
		case 4:
			switch(timeStatus4){
			case START4:
				break;
			case RUNNING4:		
				break;
			case PAUSE4:
				clickBox.setText(clickText);
				mTimer4.removeMessages(0);
				timeStatus4=START4;
				//db
				dbHelper = new DBHelper(this);
				SQLiteDatabase db = dbHelper.getWritableDatabase();
				long a = mPaseTime3-mBaseTime3;
				//db.execSQL("INSERT INTO step1 VALUES( null,'" + todayDate1 + "','" + name + "','" + a + "');");
				db.execSQL("INSERT INTO step4 VALUES( null,'" + getTodayDate2() + "','" + name + "','" + a + "','" + nowTime+"','"+ getNowTime()+ "');");
				dbHelper.close();
				break;
			}
			break;
		case 5:
			break;
		case 6:
			switch(timeStatus6){
			case START6:
				break;
			case RUNNING6:		
				break;
			case PAUSE6:
				clickBox6.setText(clickText);
				mTimer6.removeMessages(0);
				timeStatus6=START6;
				//db
				dbHelper = new DBHelper(this);
				SQLiteDatabase db = dbHelper.getWritableDatabase();
				long a = mPaseTime6-mBaseTime6;
				//db.execSQL("INSERT INTO step1 VALUES( null,'" + todayDate1 + "','" + name + "','" + a + "');");
				db.execSQL("INSERT INTO step6 VALUES( null,'" + date + "','" + name + "','" + a + "','" + nowTime2+"','"+ getNowTime()+ "');");
				dbHelper.close();
				break;
			}
			break;
		}
		
	}//end of doNeutralClick()

	@Override
	public void onDestroy() {
		mTimer.removeMessages(0);
		mTimer2.removeMessages(0);
		mTimer3.removeMessages(0);
		mTimer4.removeMessages(0);
		mTimer6.removeMessages(0);
		super.onDestroy();
	}//onDestroy()
	
	//1단계
	private String startTime(){
		long now = SystemClock.elapsedRealtime();
		time = now - mBaseTime;
		String timeString = String.format("%02d:%02d:%02d", time/1000/60/60, (time/1000/60)%60, (time/1000)%60);
		return timeString;	
	}//startTime()
	
	//2-1단계
	private String startTime2(){
		long now2 = SystemClock.elapsedRealtime();
		time2 = now2 - mBaseTime2;
		String timeString2 = String.format("%02d:%02d:%02d", time2/1000/60/60, (time2/1000/60)%60, (time2/1000)%60);
		return timeString2;	
	}//startTime2()
	
	//2-2단계
	private String startTime3(){
		long now3 = SystemClock.elapsedRealtime();
		time3 = now3 - mBaseTime3;
		String timeString3 = String.format("%02d:%02d:%02d", time3/1000/60/60, (time3/1000/60)%60, (time3/1000)%60);
		return timeString3;	
	}//startTime2()
	
	//3단계
	private String startTime4(){
		String minute = getResources().getString(R.string.class_maintime_minute);
		long now4 = SystemClock.elapsedRealtime();
		time4 = now4 - mBaseTime4;
		String timeString4 = String.format("%d"+minute,time4/1000/60);
		return timeString4;	
	}//startTime4()
	
	//4단계
	private String startTime6(){
		String minute = getResources().getString(R.string.class_maintime_minute);
		long now6 = SystemClock.elapsedRealtime();
		time6 = now6 - mBaseTime6;
		String timeString6 = String.format("%d"+minute, time6/1000/60);
		return timeString6;	
	}//startTime4()	
	
	Handler mTimer = new Handler(){
		public void handleMessage(Message msg) {
			/*//날짝 변동되고 처음 확인한 순간 이전 시간까지 저장하고 다시 기록을 시작하게끔 하는 소스
			String firstTime = getResources().getString(R.string.class_maintime_first_time);
			String clickText = getResources().getString(R.string.class_maintime_click);*/
			
			if(mBaseDate1.equals(getTodayDate())){
				//Toast.makeText(MainTime.this, "true", Toast.LENGTH_LONG).show();
				timeText.setText(startTime());
				mTimer.sendEmptyMessageDelayed(0, 500);
			}else{
				//날짜 변경상태를 저장
				dateStatus1=dateStatus1+1;
				SharedPreferences pref_dateStatus1 = getSharedPreferences("dateStatus",0);
				SharedPreferences.Editor editor_dateStatus1 = pref_dateStatus1.edit();
				editor_dateStatus1.putInt("dateStatus1", dateStatus1);
				editor_dateStatus1.commit();
				//날짜가 변경된후 비교하기위한 데이터를 오늘날짜로 변경
				mBaseDate1 = getTodayDate();
				SharedPreferences pref_mBaseDate = getSharedPreferences("mBaseDate",0);
				SharedPreferences.Editor editor_mBaseDate = pref_mBaseDate.edit();
				editor_mBaseDate.putString("mBaseDate1", mBaseDate1);
				editor_mBaseDate.commit();
				
				/*//날짝 변동되고 처음 확인한 순간 이전 시간까지 저장하고 다시 기록을 시작하게끔 하는 소스
				mPaseTime = SystemClock.elapsedRealtime();
				timeText.setText(firstTime);
				//활동명을 불러와서 db에 저장하기
				SharedPreferences pref_inputAct = getSharedPreferences("inputAct", 0);
				String runningName = pref_inputAct.getString("LastAct", clickText);
				dbHelper = new DBHelper(MainTime.this);
				SQLiteDatabase db = dbHelper.getWritableDatabase();
				long a = mPaseTime-mBaseTime;
				db.execSQL("INSERT INTO step1 VALUES( null,'" + mBaseDate1 + "','" + runningName + "','" + a + "');");
				dbHelper.close();
				//다시 시작하기
				mBaseTime = mPaseTime;
				mBaseDate1 = getTodayDate();
				SharedPreferences pref_mBaseDate = getSharedPreferences("mBaseDate",0);
				SharedPreferences.Editor editor_mBaseDate = pref_mBaseDate.edit();
				editor_mBaseDate.putString("mBaseDate1", mBaseDate1);
				editor_mBaseDate.commit();
				mTimer.sendEmptyMessageDelayed(0, 500);*/
				
			}
		}	
	};//Handler()
	
	Handler mTimer2 = new Handler(){
		public void handleMessage(Message msg) {	
				timeText2.setText(startTime2());
				mTimer2.sendEmptyMessageDelayed(0, 500);		
		}
	};//Handler2()

	Handler mTimer3 = new Handler(){
		public void handleMessage(Message msg) {	
				timeText3.setText(startTime3());
				mTimer3.sendEmptyMessageDelayed(0, 500);		
		}
	};//Handler3()
	
	Handler mTimer4 = new Handler(){
		public void handleMessage(Message msg) {
				String firstActName = getResources().getString(R.string.class_maintime_first_actname);
				//활동명 프레퍼런스에서 읽어오기
				SharedPreferences pref_inputAct4 = getSharedPreferences("inputAct4", 0);
				nameText4 = pref_inputAct4.getString("LastAct4", firstActName);
				int lastIndex = arList3.size()-1;
				arList3.set(lastIndex, new MyItems3(nameText4,startTime4(),nowTime,"---",getNowTime()));
				MyListAdapter3 myAdapter3 = new MyListAdapter3(MainTime.this, R.layout.customlistview3, arList3);
				ListView ThirdStepList = (ListView)findViewById(R.id.third_step_list);
				ThirdStepList.setAdapter(myAdapter3);
				mTimer4.sendEmptyMessageDelayed(0, 10000);		
		}	
	};//Handler4()
	
	Handler mTimer6 = new Handler(){
		public void handleMessage(Message msg) {
				String firstActName = getResources().getString(R.string.class_maintime_first_actname);
				SharedPreferences pref_inputAct6 = getSharedPreferences("inputAct6", 0);
				nameText6 = pref_inputAct6.getString("LastAct6", firstActName);
				int lastIndex = arList2.size()-1;
				arList2.set(lastIndex, new MyItems3(nameText6,startTime6(),nowTime2,"---",getNowTime()));
				MyListAdapter2 myAdapter6 = new MyListAdapter2(MainTime.this, R.layout.customlistview2, arList2);
				ListView FourthStepList = (ListView)findViewById(R.id.fourth_step_list2);
				FourthStepList.setAdapter(myAdapter6);
				mTimer6.sendEmptyMessageDelayed(0, 10000);		
		}	
	};//Handler6()
	
}
